package manage.controller;

import com.alibaba.fastjson.JSONObject;
import manage.base.BaseController;
import manage.model.Role;
import manage.service.RoleServcie;
import manage.service.impl.RoleServiceImpl;
import manage.util.Msg;
import manage.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @author: create by  Raphaelkxy
 * @version: v1.0
 * @description: manage.controller
 * @date:2019/8/26
 */
@Controller
public class Rolecontroller extends BaseController {


    @Autowired
    private RoleServcie roleService;


    /**
     * 根据id获取role
     * @param request
     * @return
     */
    @RequestMapping(value = "/getrolebyid",produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public String getRoleById(@RequestBody  Integer id){
        Role role = roleService.getbyid(id);
        String returninfo = JSONObject.toJSONString(role);
        return returninfo;
    }
    //查询业业务
    @RequestMapping(value = "/getrolelist",produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public String getRoleList(HttpServletRequest request){
        String pageSize = request.getParameter("pageSize");
        String currentPage = request.getParameter("currentPage");
        PageInfo page = getPage(pageSize, currentPage);
        List<Role>roles = roleService.getALl();
        validate(page);
        HashMap result = new HashMap();
        result.put("arrayList", roles);
        result.put("pageInfo", page);
        String returnInfo = JSONObject.toJSONString(result);
        return returnInfo;
    }
    //插入业务
    @ResponseBody
    @RequestMapping(value = "/insertrole",method = RequestMethod.POST)
    public String insertRole(Role role,HttpSession session){
        int i=roleService.saveRole(role);
        if (i > 0){
            List<Role>rolesnew =roleService.getALl();
            session.removeAttribute("roles");
            session.setAttribute("roles",rolesnew);
            return "添加成功";}
        else
            return "添加失败";
    }

  //更新业务
    @ResponseBody
    @RequestMapping(value = "/insertrole",method = RequestMethod.PUT)
    public String updateRole(@RequestBody Role role,HttpSession session){
       // HttpSession httpSession =
        int i=roleService.updateRole(role);
        if(i>0){
            String returnInfo1 = JSONObject.toJSONString("success");
            List<Role>rolesnew =roleService.getALl();
            session.removeAttribute("roles");
            session.setAttribute("roles",rolesnew);
            return returnInfo1;
        }else {
            String returnInfo1 = JSONObject.toJSONString("fail");
            return returnInfo1;
        }
    }
    //删除业务
    @ResponseBody
    @RequestMapping(value = "/deleteRole",produces = "text/html;charset=UTF-8",method = RequestMethod.DELETE)
    public String delRole(@RequestBody String id){
        int i = roleService.deleteByid(Integer.parseInt(id));
        if(i>0){
            String returnInfo = JSONObject.toJSONString("success");
            return returnInfo;
        }else {
            String returnInfo = JSONObject.toJSONString("fail");
            return returnInfo;
        }
    }
}
